package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.TeacherNameMapper;
import com.example.programming.sys.entity.TeacherName;

import org.springframework.web.bind.annotation.RestController;
import com.example.programming.common.tools.BaseController;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/sys/teacher-name")
public class TeacherNameController extends BaseController {
    @Autowired
    private TeacherNameMapper teacherNameMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<TeacherName> page = new Page<TeacherName>(pageNum, pageSize);
        TeacherName parms = new TeacherName();
        QueryWrapper<TeacherName> warpper = new QueryWrapper<TeacherName>(parms);

        result.setData(teacherNameMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/getname")
    public Result getname(
            String nickname) {
        System.out.println("nickname:"+nickname);
        Result result = new Result();
        TeacherName teacherName = new TeacherName();
        teacherName.setNickname(nickname);
        TeacherName teacherNameExit = teacherNameMapper.selectOne(new QueryWrapper<TeacherName>(teacherName));

        result.setData(teacherNameExit);
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody TeacherName entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            teacherNameMapper.insert(entity);
        } else {
            teacherNameMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        teacherNameMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
